package com.turman.blibrary.log;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.List;

/**
 * B日志打印工具
 *
 * @author buobao
 * @since 2024/2/26 17:09
 */
public class BLog {
    private static final String B_LOG_PACKAGEN;

    static {
        String packageName = BLog.class.getName();
        B_LOG_PACKAGEN = packageName.substring(0, packageName.lastIndexOf(".") + 1);
    }

    public static void v(Object... contents) {
        log(BLogType.V, contents);
    }

    public static void vt(@NonNull String tag, Object... contents) {
        log(BLogType.V, tag, contents);
    }

    public static void i(Object... contents) {
        log(BLogType.I, contents);
    }

    public static void it(@NonNull String tag, Object... content) {
        log(BLogType.I, tag, content);
    }

    public static void w(Object... contents) {
        log(BLogType.W, contents);
    }

    public static void wt(@NonNull String tag, Object... content) {
        log(BLogType.W, tag, content);
    }

    public static void a(Object... contents) {
        log(BLogType.A, contents);
    }

    public static void at(@NonNull String tag, Object... content) {
        log(BLogType.A, tag, content);
    }

    public static void e(Object... contents) {
        log(BLogType.E, contents);
    }

    public static void et(@NonNull String tag, Object... content) {
        log(BLogType.E, tag, content);
    }

    public static void log(@BLogType.TYPE int type, Object... contents) {
        log(type, BLogManager.getInstance().getConfig().getGlobalTag(), contents);
    }

    public static void log(@BLogType.TYPE int type, @NonNull String tag, Object... contents) {
        log(BLogManager.getInstance().getConfig(), type, tag, contents);
    }

    public static void log(@NonNull BLogConfig config, @BLogType.TYPE int type, @NonNull String tag, Object... contents) {
        if (!config.enable()) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        String body = parseBody(contents, config);
        if (config.showThread()) { //打印线程信息
            String threadInfo = BLogConfig.THREAD_FORMATTER.format(Thread.currentThread());
            sb.append(threadInfo);
        }
        if (config.staceTraceDepth() > 0) {
            String stackTrace = BLogConfig.STACK_TRACE_FORMATTER.
                    format(BStackTraceUtil.getCorppedRealStackTrace(new Throwable().getStackTrace(), B_LOG_PACKAGEN, config.staceTraceDepth()));
            sb.append(stackTrace).append("\n");
        }
        sb.append(body);
        List<BLogPrinter> printers = config.printers() != null ? Arrays.asList(config.printers()) : BLogManager.getInstance().getPrinters();
        if (printers == null) {
            return;
        }
        for (BLogPrinter printer : printers) {
            printer.print(config, type, tag, sb.toString());
        }
    }

    private static String parseBody(@NonNull Object[] contents, @NonNull BLogConfig config) {
        StringBuilder sb = new StringBuilder();
        for (Object content : contents) {
            if (content instanceof String) { //如果是字符串直接打印
                sb.append(content).append("\n");
            } else { //当不是字符串时通过设定的json转换器打印
                if (config.injectJsonParser() != null) {
                    sb.append(config.injectJsonParser().toJson(content)).append("\n");
                } else {
                    sb.append(content.toString()).append(";");
                }
            }
        }

        return sb.toString();
    }
}





















